﻿using System;
using System.Collections.Generic;

namespace Cms.Domain
{
    [Serializable]
    public class ApplicationPage : Page
    {
        private Application _application;
        private IDictionary<string, SourceCode> _areas;
        private SourceCode _sourceCode;

        public ApplicationPage()
        {
            _areas = new Dictionary<string, SourceCode>();
        }

        public virtual SourceCode SourceCode
        {
            get { return _sourceCode; }
            set { _sourceCode = value; }
        }
        
        public virtual IDictionary<string, SourceCode> Areas
        {
            get { return _areas; }
            set { _areas = value; }
        }
        
        public virtual Application Application
        {
            get { return _application; }
            set { _application = value; }
        }

        public virtual string GetAreaDesign(string area)
        {
            return _areas.ContainsKey(area) ? _areas[area].CurrentVersion.Code : string.Empty;
        }
        
        public virtual void SetAreaDesign(string area, string code, Administrator editedBy)
        {
            SourceCode sc;
            if (!_areas.ContainsKey(area))
            {
                sc = new SourceCode();
                sc.Site = this.Site;
                _areas.Add(area, sc);
            }
            else
                sc = _areas[area];
            sc.AddVersion(code, editedBy);
        }
        
        public virtual string GetCode()
        {
            return _sourceCode != null ? _sourceCode.CurrentVersion.Code : string.Empty;
        }
        
        public virtual void SetCode(string code, Administrator editedBy)
        {
            if (_sourceCode == null)
            {
                _sourceCode = new SourceCode();
                _sourceCode.Site = this.Site;
            }
            _sourceCode.AddVersion(code, editedBy);
        }
    }
}